package com.hhd.pdf.components.table.factory;


import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.hhd.pdf.components.table.TableBorderHelper;
import com.hhd.pdf.components.table.TableVo;
import com.hhd.pdf.components.table.TdVo;
import com.hhd.pdf.components.table.TrVo;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.hhd.pdf.CommonUtils;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.io.IOException;
import java.util.List;

@Getter
@Setter
@Builder(builderMethodName = "headBuilder")
public class TableHeaderFactory extends BaseFactory {
    PdfPTable pdfPTable;
    GeneratorContext context;
    TableVo tableVo;
    TableBorderHelper tableBorderHelper;

    @Override
    public void create() throws DocumentException, IOException {
        List<TrVo> header = tableVo.getHeader();
        if (CommonUtils.isEmpty(header)) return;

        doCreate(header);
    }

    void doCreate(List<TrVo> trs) throws DocumentException, IOException {
        for (TrVo trVo : trs) {
            List<TdVo> tds = trVo.getTds();
            for (TdVo td : tds) {
                PdfPCell pdfPCell = buildCell(td, tableBorderHelper, tableVo, context);
                resolveHeaderTdStyle(trVo, pdfPCell);
                resolveSimpleTdContent(pdfPCell, td, context);

                pdfPTable.addCell(pdfPCell);
                tableBorderHelper.nextCol();
            }
            tableBorderHelper.nextRow();
        }
    }

    private void resolveHeaderTdStyle(TrVo trVo, PdfPCell cell) {
        if (trVo.getType() == TrVo.TrType.head ||
                trVo.getType() == TrVo.TrType.foot) {
            cell.setBackgroundColor(PdfUtils.createColor(250, 250, 250));
        }
    }

    protected PdfPCell buildCell(TdVo td,
                                 TableBorderHelper tableBorderHelper,
                                 TableVo tableVo,
                                 GeneratorContext context) {
        PdfPCell cell = PdfUtils.createCell();
        cell.setPaddingLeft(5);
        cell.setPaddingRight(5);
        cell.setPaddingTop(0);
        cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
        cell.setColspan(td.getColSpan());

        cell.setHorizontalAlignment(td.getAlign());
        cell.setMinimumHeight(35);
        tableBorderHelper.setBorder(cell, tableVo, context);

        resolveTdStyle(td, cell);


        setPadding(td, cell);

        return cell;
    }

    private static void resolveTdStyle(TdVo td, PdfPCell cell) {
        try {
            Attribute type = td.getTd().attribute("type");
            if (type.getText().equals("header")) {
                cell.setBackgroundColor(PdfUtils.createColor(250, 250, 250));
            }
        } catch (Exception e) {
        }
    }

    private static void setPadding(TdVo td, PdfPCell cell) {
        try {
            Attribute padding = td.getTd().attribute("padding");
            cell.setPadding(Integer.parseInt(padding.getValue()));
        } catch (Exception e) {
        }
    }

    private void resolveSimpleTdContent(PdfPCell pdfPCell,
                                        TdVo td,
                                        GeneratorContext context) throws DocumentException, IOException {
        Element td1 = td.getTd();
        List<com.itextpdf.text.Element> elements = parseSimpleNode(td1, context);
        Paragraph paragraph = PdfUtils.createParagraph(context.getGeneratorParameters().getFont(), td.getAlign());
        for (com.itextpdf.text.Element element : elements) {
            if (PdfUtils.isPhrase(element)) {
                Phrase phrase = PdfUtils.toPhrase(element);
                phrase.setFont(context.getGeneratorParameters().getFont());
            }
            paragraph.add(element);
        }

        pdfPCell.addElement(paragraph);
    }
}
